local cjson = require('cjson')
local client = require "resty.kafka.client"
local producer = require "resty.kafka.producer"
local bigdatatool = require("resty.bigdatatool")
local uncompress = bigdatatool.uncompress
local uaparse = require("resty.uaparse")

local args = ngx.req.get_uri_args()
ngx.req.read_body()
local pargs = ngx.req.get_post_args()
local data_list = pargs.data_list
--local ext = args.ext
local project = args.project
local gzip = tonumber(pargs.gzip)
local crc = tonumber(pargs.crc)

--校验项目名称
if not bigdatatool.isMatch(project or '','^(production|default)$') then
    --ngx.exit(403)
    return
end

local broker_list = {
    {
        host = "192.168.0.1",
        port = 9092
    },{
        host = "192.168.0.1",
        port = 9092
    }
}
--解决消息没有产生时间问题
producer.zsapi_version = 2
local bp = producer:new(broker_list, { api_version=2})

--数据校验不通过停止往下执行
if crc ~= nil and crc ~= bigdatatool.hashCode(data_list) then
    --收集未通过crc验证数据信息
    bp:send('not_match_sa',nil, cjson.encode({data=pargs,src='sa'}))
    --ngx.exit(403)
    return
end


local decodest = pcall(function()
    data_list = ngx.decode_base64(data_list)
end)

if not decodest then
    return
end

local ip = bigdatatool.getip()
local addr = {}
xpcall(function()
    --解析ip所在地区
    local res,err = bigdatatool.ipfind(ip)
    if err then
        ngx.log(ngx.ERR,err)
    else
        addr = {province=res.region_name,city=res.city_name,country=res.country_name,isp_domain=res.isp_domain}
    end
end,function(err)
    ngx.log(ngx.ERR,tostring(err))
end)
if gzip == 1 then
    local uss,ret,getin,getout = uncompress(data_list)
    data_list = uss
end

local ua = ngx.var.http_user_agent
local uap = {}
pcall(function ()
    --浏览器解析出的信息
    uap = uaparse.parse(ua)
end)
local pattern = [[("[$\-\w]+":)(\d{15,})]]
local replacement = [[$1"$2"]]
local new_json, err = ngx.re.gsub(data_list, pattern, replacement)
--lua int64 处理
local arr = cjson.decode(new_json)

for i=1,#arr do
    arr[i].project = project
    arr[i].ip = ip
    arr[i].recv_time = ngx.time()
    arr[i].ua = ua
    arr[i].uap = uap
    arr[i].addr = addr
    bigdatatool.verifytime(arr[i])
    pcall(function()
        if arr[i].properties['$os'] == nil then
            arr[i].properties['$os'] = uap.os
        end
        if arr[i].properties['$os_version'] == nil then
            arr[i].properties['$os_version'] = uap.os_version
        end
        if arr[i].properties['$country'] == nil then
            arr[i].properties['$country'] = addr.country
        end
        if arr[i].properties['$province'] == nil then
            arr[i].properties['$province'] = addr.province
        end
        if arr[i].properties['$city'] == nil then
            arr[i].properties['$city'] = addr.city
        end
        if arr[i].properties.platform_type == nil and (arr[i].properties['$lib'] == 'Android' or uap.os == 'Android') then
            arr[i].properties.platform_type = 'Android'
        end
    end)
    local topic = 'bigdata_event'
    if bigdatatool.isMatch(arr[i].type,'^profile_') then
        topic = 'bigdata_user'
    elseif bigdatatool.isMatch(arr[i].type,'^item_') then
        topic = 'bigdata_item'
    end
    local ok, err = bp:send(topic, nil, cjson.encode(arr[i]))
    if not ok then
        ngx.log(ngx.ERR,err)
    end
end